const MyPromise = require('./MyPromise')

let p = new MyPromise(function(resolve,reject){
    setTimeout(function(){
        resolve(100)
    },2000)
});

p.then(value => console.log(value),reason => console.log(reason))
p.then(value => console.log(value),reason => console.log(reason))


let p1 = new MyPromise(function(resolve,reject){
    setTimeout(function(){
        resolve(200)
    },2000)
});
p1.then(value =>{
    console.log(value)
    return 300;
}).then(value =>{
    console.log(value)
});


let p2 = new MyPromise(function(resolve,reject){
    setTimeout(function(){
        resolve(400)
    },2000)
});
p2.then(value=>{
    console.log(value)
    return new MyPromise(function(mResolve,mReject){
        setTimeout(function(){
            mResolve(500)
        },2000)
    })
}).then(value=>{
    console.log(value)
});


let p3 = new MyPromise(function(resolve,reject){
    setTimeout(function(){
        resolve('finally success')
    },4000)
});
p3.finally(()=>{
    console.log('finally')
    //测试先执行完finally里的方法再继续向下执行
    return new MyPromise(function(resolve,reject){
        setTimeout(function(){
            resolve('finally success2')
        },4000)
    }).then(value=> console.log(value));
}).then(value=>{
    console.log(value)
},reason=>{
    console.log(reason)
})

function all1(){
    return new MyPromise(function(resolve,reject){
        setTimeout(function(){
            resolve('all1')
        },10000)
    })
}

function all2(){
    return new MyPromise(function (resolve,reject){
        resolve('all2')
    })
}

MyPromise.all(['all0',all1(),all2()]).then(function(value){
    console.log(value)
})